package com.imagination.applet.domain.virtualidentity.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.imagination.applet.controller.api.enums.TrainingStatusEnum;
import com.imagination.applet.domain.virtualidentity.model.UserVirtualIdentity;
import com.imagination.applet.domain.virtualidentity.repository.UserVirtualIdentityRepository;
import com.imagination.applet.domain.virtualidentity.service.UserVirtualIdentityCommandService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class UserVirtualIdentityServiceImpl implements UserVirtualIdentityCommandService {

    private final UserVirtualIdentityRepository userVirtualIdentityRepository;

    @Override
    public UserVirtualIdentity createVirtualIdentity(Long userId) {
        UserVirtualIdentity entity = UserVirtualIdentity.builder()
                .userId(userId)
                .trainingStatus(TrainingStatusEnum.TRAINING_RUNNING.getCode())
                .build();
        return userVirtualIdentityRepository.save(entity);
    }

    @Override
    public List<UserVirtualIdentity> queryTrainingSuccessList(Long userId) {
        return userVirtualIdentityRepository.queryByUserIdAndTrainingStatusAndDeleted(userId,
                TrainingStatusEnum.TRAINING_SUCCESS.getCode(), 0);
    }

    @Override
    public List<UserVirtualIdentity> queryTrainingSuccessListContainDel(Long userId) {
        return userVirtualIdentityRepository.queryByUserIdAndTrainingStatus(userId,
                TrainingStatusEnum.TRAINING_SUCCESS.getCode());
    }

    @Override
    public List<UserVirtualIdentity> queryTrainingRunning(Long userId) {
        return userVirtualIdentityRepository.queryByUserIdAndTrainingStatusAndDeleted(userId,
                TrainingStatusEnum.TRAINING_RUNNING.getCode(), 0);
    }

    @Override
    public List<UserVirtualIdentity> queryTrainingRunning() {
        return userVirtualIdentityRepository.queryByTrainingStatusAndDeleted(
                TrainingStatusEnum.TRAINING_RUNNING.getCode(), 0);
    }

    @Override
    public UserVirtualIdentity getById(Long id) {
        return userVirtualIdentityRepository.getReferenceById(id);
    }

    @Override
    public void succeed(Long virtualIdentityId, Map<String, Object> extensions) {
        UserVirtualIdentity identity = userVirtualIdentityRepository.getReferenceById(virtualIdentityId);
        identity.setTrainingStatus(TrainingStatusEnum.TRAINING_SUCCESS.getCode());
        identity.setExtensions(JSONObject.toJSONString(extensions));
        userVirtualIdentityRepository.save(identity);
    }

    @Override
    public void failed(Long virtualIdentityId) {
        UserVirtualIdentity identity = userVirtualIdentityRepository.getReferenceById(virtualIdentityId);
        identity.setTrainingStatus(TrainingStatusEnum.TRAINING_FAILED.getCode());
        userVirtualIdentityRepository.save(identity);
    }

    @Override
    public void remove(Long id) {
        userVirtualIdentityRepository.deleteById(id);
    }
}
